<template>
  <div>
    <el-form :inline="true" :model="queryForm" @keyup.enter.native="getDataList('reset')">
      <el-form-item label="政策标题">
        <el-input v-model="queryForm.header" maxlength="20" placeholder="政策标题" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getDataList('reset')">查询</el-button>
        <el-button icon="el-icon-refresh-left" @click="reset">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" @click="openDialog()">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%;">
      <el-table-column align="center" label="序号" width="180">
        <template slot-scope="scope">
          <span>{{(queryForm.page - 1) * queryForm.limit + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="header" header-align="center" align="center" label="政策标题">
      </el-table-column>
      <el-table-column prop="isShow" header-align="center" align="center" label="显示状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.is_show">显示</el-tag>
          <el-tag type="warning" v-else>隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" header-align="center" align="center" label="更新时间">
        <template slot-scope="{row}">
          {{row.update_time||row.create_time}}
        </template>
      </el-table-column>
      <el-table-column prop="sort" header-align="center" align="center" label="排序">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="{row}">
          <el-button type="text" size="small" @click="openDialog(row)">修改</el-button>
          <el-button type="text" size="small" @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="queryForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="queryForm.limit" :total="total" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 新增、编辑弹窗 -->
    <el-dialog :title="form.id?'修改':'新增'" top="5vh" center :visible.sync="dialogVisible" width="60%" :before-close="handleClose">
      <div>
        <el-form :model="form" v-loading="dialogLoading" ref="form" label-width="100px" :rules="rules">
          <el-form-item label="政策名称：" prop="header">
            <el-input v-model="form.header" maxlength="20" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="缩略图：" prop="thumbnail">
            <uploadImg @updataImg="updataImg" :limit="1" :value="backImg" :dialogVisible="dialogVisible" style="" :zi='true'></uploadImg>
          </el-form-item>
          <el-form-item label="序号：" prop="sort">
            <el-input-number v-model="form.sort" :precision="0" :min="0"></el-input-number>
          </el-form-item>
          <el-form-item label="显示状态：" prop="sort">
            <el-switch v-model="form.is_show"></el-switch>
          </el-form-item>
          <el-form-item label="内容：" prop="content">
            <div>
              <editor :heightMultiple="1.1" height="400" :value="form.content" @getTxt="getEditor"></editor>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import workpolicyApi from '@/api/workpolicyApi.js'
import editor from '@/components/editor.vue'
import uploadImg from '@/views/modules/oss/oss-uploadImg2'
export default {
  components: {
    editor,
    uploadImg
  },
  data () {
    return {
      queryForm: {
        page: 1,
        limit: 10,
      },
      dataListLoading: true,
      dataList: [],
      total: 0,
      dialogVisible: false,
      form: {
        content: "",
        thumbnail: ''
      },
      rules: {
        header: [
          { required: true, message: '请输入政策标题', trigger: 'change' }
        ],
        content_url: [
          { required: true, message: '请上传图片', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'change' }
        ]
      },
      btnLoading: false,
      dialogLoading: false,
      activeItem: {},
      backImg: []
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    // 获取列表
    getDataList (reset) {
      if (reset) this.queryForm.page = 1
      this.dataListLoading = true;
      workpolicyApi.workpolicyPage(this.queryForm).then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list;
          this.total = res.data.page.totalCount;
        }
        this.dataListLoading = false;
      })
    },
    // 获取详情
    getDetails () {
      this.dialogLoading = true;
      workpolicyApi.workpolicyInfo(this.activeItem.id).then(res => {
        if (res && res.data.code == 200) {
          this.form = res.data.data;
          this.backImg = [this.form.thumbnail]
        }
        this.dialogLoading = false
      })
    },
    reset () {
      this.queryForm = {
        limit: 10,
        page: 1,
      }
      this.getDataList()
    },
    // 打开弹窗
    openDialog (item) {
      this.activeItem = item || {};
      this.dialogVisible = true;
      if (item) {
        this.getDetails()
      }
    },
    // 关闭弹窗
    handleClose () {
      this.$refs.form.resetFields();
      this.form = {};
      this.dialogVisible = false;
    },
    // 富文本的回调
    getEditor (e) {
      this.form.content = e
    },
    // 每页数
    sizeChangeHandle (val) {
      this.queryForm.limit = val
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.queryForm.page = val
      this.getDataList()
    },
    //上传之后
    updataImg (e) {
      this.form.thumbnail = e[0]
      // this.backImg = e
    },
    // 删除
    del (item) {
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        workpolicyApi.workpolicyDel(item.id).then(res => {
          if (res && res.data.code == 200) {
            this.$message.success('删除成功！')
            this.reset()
          }
        })
      }).catch(() => { })
    },
    // 提交
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.activeItem.id) {
            this.update()
          } else {
            this.add()
          }
        }
      })
    },
    // 新增
    add () {
      this.btnLoading = true;
      workpolicyApi.workpolicyAdd(this.form).then(res => {
        this.btnLoading = false;
        if (res && res.data.code == 200) {
          this.$message.success('新增成功！');
          this.getDataList()
          this.handleClose()
        }
      })
    },
    // 修改
    update () {
      this.btnLoading = true;
      workpolicyApi.workpolicyUpdate(this.form).then(res => {
        this.btnLoading = false;
        if (res && res.data.code == 200) {
          this.$message.success('修改成功！');
          this.getDataList()
          this.handleClose()
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
</style>